"""

 Generic utils module
 $Id: utils.py 457 2012-12-28 20:24:27Z akelge@gmail.com $

"""
from model import *
from ldapImport import syncLDAPUsers
import views

# users=session.query(cmUser).all()
# domains=session.query(cmDomain).all()
# aliases=session.query(cmAlias).all()

# d0=domains[0]
# d1=domains[1]
# d2=domains[2]

def createSchema():
    """
    By now we parse views from views.py, using 0.6 we could build them
    """
    views.dropViews()
    metadata.drop_all(engine)
    metadata.create_all(engine)
    views.createViews()

def cubeInit():
    conf=cmConfiguration()
    conf.ldap_url='ldap://od.example.lan'
    conf.admin_dn='uid=xxxx,cn=users,dc=cube,dc=lan'
    conf.secret='yyyyy'
    conf.base_dn='cn=users,dc=cube,dc=lan'
    conf.filter='(&(objectClass=posixAccount)(gidNumber=5000))'

    dotCom=cmDomain(name='example.com')
    dotIt=cmDomain(name='example.org', primary_domain=dotCom)
    session.add(conf)
    session.commit()
    session.add(dotCom)
    session.add(dotIt)
    session.commit()
    for user in syncLDAPUsers():
        session.add(user)
    # Add test user
    session.add(cmUser(username='test', domain=dotCom, password='testXzy'))
    session.commit()



